package com.pzb.web.controller.market;

import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.core.page.TableDataInfo;
import com.pzb.market.domain.MkCoupon;
import com.pzb.market.service.IMkCouponService;
import com.pzb.wechat.domain.WechatUserCoupon;
import com.pzb.wechat.service.IWechatUserCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券Controller
 * 
 * @author pzb
 * @date 2021-10-11
 */
@RestController
@RequestMapping("/coupon")
public class MkCouponController extends BaseController
{
    @Autowired
    private IMkCouponService mkCouponService;
    @Autowired
    private IWechatUserCouponService userCouponService;

    /**
     * 查询优惠券列表
     */
    @GetMapping("/list")
    public AjaxResult couponList(@RequestParam("extAppid") String extAppid)
    {
        List<MkCoupon> list = mkCouponService.selectMkCouponList(extAppid,getOpenid());
        return AjaxResult.success(list);
    }

    /**
     * 我的优惠券优惠券列表
     */
    @GetMapping("/myCouponList")
    public TableDataInfo myCouponList(@RequestParam("extAppid") String extAppid, @RequestParam("dataType")String dataType, @RequestParam("pageNum") Integer pageNum)
    {
        startPage();
        List<WechatUserCoupon> userCouponList = userCouponService.selectWechatUserCouponList(extAppid,getOpenid(),dataType);
        return getDataTable(userCouponList);
    }

    /**
     * 领取优惠券
     */
    @PostMapping("/receive")
    public AjaxResult receive(@RequestBody WechatUserCoupon userCoupon)
    {
        userCoupon.setOpenid(getOpenid());
        int  count = userCouponService.insertWechatUserCoupon(userCoupon);
        if(count == 0){
            return AjaxResult.error("对不起，已经抢光了");
        }
        return toAjax(count);
    }



}
